package com.lixin.smartclassroom.ui.login

import android.graphics.Color
import android.widget.ImageView
import android.widget.TextView
import com.drake.brv.utils.bindingAdapter
import com.drake.net.utils.scopeDialog
import com.drake.net.utils.scopeNetLife
import com.drake.tooltip.dialog.BubbleDialog
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.bindstudentlist
import com.lixin.smartclassroom.api.searchcustomer
import com.lixin.smartclassroom.api.unbindstudent
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.bindStudentDialog
import com.lixin.smartclassroom.databinding.ActivityBindStudentBinding
import com.lixin.smartclassroom.databinding.ActivityRegisterBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.child_name
import com.lixin.smartclassroom.ui.MainActivity
import com.lixin.smartclassroom.ui.home.adapter.bindStudent
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.color
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.removeAt
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import per.goweii.anylayer.AnyLayer
import per.goweii.anylayer.ktx.doOnClickToDismiss

/**
 * Description 绑定学生
 * Author: lwc
 * Date:2023/12/6 8:39
 */
class BindStudentAct : BaseActivity<ActivityBindStudentBinding>() {
    override fun getViewBinding() = ActivityBindStudentBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "绑定学生"
    private lateinit var listBean: ArrayList<DataListBean>
    private var bindType = "1"//1未绑定  2已绑定
    private var fromType = "1"//1首页  2个人中心-我的班级
    private var currentType = 0

    override fun iniView() {
        bindType = getIntentString("bindType")
        fromType = getIntentString("type")
        binding.btnSure.setRoundRectBg(
            Color.parseColor("#4B6AEF"),
            dp2px(27.5f).toInt()
        )

        if (bindType == "1")
            setNoBindUI()
        else
            setHasBindUi()


        scopeNetLife {
            listBean = bindstudentlist(1, 1000)!!
            binding.recycler.bindStudent(fromType).apply {
                R.id.btnCancel.onClick {
                    val dialog = BubbleDialog(this@BindStudentAct)
                    scopeDialog(dialog) {
                        unbindstudent((getModel(modelPosition) as DataListBean).id!!)
                        if (binding.recycler.bindingAdapter.models!!.size > 1) {
                            binding.recycler.bindingAdapter.removeAt(modelPosition)
                            child_id = (getModel(0) as DataListBean).id!!
                            child_name = (getModel(0) as DataListBean).name!!
                        } else {
                            bindType = "1"
                            setNoBindUI()
                            child_id = ""
                            child_name = ""
                        }
                    }
                }

            }.models = listBean
        }
    }

    override fun onBlackListener() {
        when (currentType) {
            0 -> finish()
            1 -> {
                setHasBindUi()
                currentType = 0
            }
        }
    }

    private fun setNoBindUI() {
        binding.apply {
            llHasBind.gone()
            llNoBind.visible()
            btnSure.text = "确定"
            bindType = "1"
        }
    }

    private fun setHasBindUi() {
        binding.apply {
            llHasBind.visible()
            llNoBind.gone()
            btnSure.text = "绑定新生"
            bindType = "2"
        }
    }

    override fun onClick() {
        binding.apply {
            btnSure.onClick {
                if (bindType == "1") {
                    scopeNetLife {
                        val baseBean = searchcustomer(etStudentID.text(), etInteractionCode.text())
                        if (baseBean.result == "0")
                            bindStudentDialog("确定绑定该生", 1, baseBean = baseBean)
                        else
                            toast(baseBean.resultNote.toString())
                    }


                } else {
                    currentType = 1
                    setNoBindUI()
                }
            }
        }


    }


}